<?php

namespace App\Logic;


use App\Model\ActivityModel;
use App\Model\CouponsUserModel;
use App\Utility\CommonTool;
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Exception\Exception;
use EasySwoole\Pool\Exception\PoolEmpty;
use Throwable;

class CouponsLogic
{
    /**
     * 显示用户优惠券列表处理
     * @param array $data
     * @return array
     * @throws Exception
     * @throws PoolEmpty
     * @throws Throwable
     */
    public static function userCouponsList($data)
    {
        $count = DbManager::getInstance()->invoke(function ($client) use ($data) {
            return CouponsUserModel::invoke($client)->where('user_id', $data['userId'])->all();
        });

        if ($count <= 0) {
            return [];
        }

        $info = DbManager::getInstance()->invoke(function ($client) use ($data) {
            $couponsUserModel = CouponsUserModel::invoke($client);
            $field = [
                'cu.id',
                'cu.validate_time',
                'c.name',
                'c.target_money',
                'c.offer_money',
                'c.is_condition',
                'c.btype_id',
                'c.stype_id',
                'c.type',
            ];
            $couponsUserModel->field($field)
                ->alias('cu')
                ->join('coupons c', 'cu.coupons_id = c.id')
                ->where('cu.user_id', $data['userId']);

            if ($data['status'] != 1) {
                $couponsUserModel->where('cu.status', '2,3', 'IN');
                $couponsUserModel->order('cu.validate_time', 'desc');
            } else {
                $couponsUserModel->where('cu.status', '1');
                $couponsUserModel->order('cu.validate_time', 'asc');
            }

            return $couponsUserModel->all();
        });

        if (count($info) <= 0) {
            return [];
        }

        $temp = [];
        foreach ($info as $key => $value) {
            $temp[$key] = $value->toArray();

            if (floatval($value->target_money) > 0) {
                $temp[$key]['target_money_text'] = '满' . intval($value->target_money) . '元可用';
            } else {
                $temp[$key]['target_money_text'] = '无门槛';
            }

            if (intval($value->is_condition) == 2) {
                $temp[$key]['is_condition_text'] = '全场活动均可使用';
                $temp[$key]['activity_id'] = '0';
            } else {
                $temp[$key]['is_condition_text'] = '仅限【' . $value->btype_id_text . '-' . $value->stype_id_text . '】赛事使用';
                $temp[$key]['activity_id'] = strval(intval(DbManager::getInstance()->invoke(function ($client) use ($value) {
                    return ActivityModel::invoke($client)
                        ->where('btype_id', $value->btype_id)
                        ->where('stype_id', $value->stype_id)
                        ->where('status', 1)
                        ->where('start_date', date('Y-m-d'), '>')
                        ->order('id', 'asc')->limit(1)
                        ->val('id');
                })));
            }

            $temp[$key]['validate_time_text'] = '有效期至 ' . date('Y-m-d', $value->validate_time);
        }

        return CommonTool::intToString($temp, [
            'btype_id' => CommonTool::getAllTypeMap(1),
            'stype_id' => CommonTool::getAllTypeMap(2),
            'type' => CommonTool::getCouponsTypeMap(),
        ]);
    }
}
